// SPA_Main.cpp :	General interface that generate choice system.
//					1) Get path to source code and invoke parser with it to start parsing.
//					2) Return from parser Succeed ? Invoke query UI : exit
// Author: Heng Belson
//

#include "Parser.h"
#include "DesignExtractor.h"
//#include "QueryEvaluator.h"
#include "SplitDeclaration.h"

int main() {
	AST *ast = new AST();
	COLLECTION list, output;
	COLLECTION *var = &list;

	STATUS status = STATUS_Not_Processed;
	STRING source(""), choice("-1"), query(""), declaration("");
	cout << "\t\t Welcome to Static Program Analyzer (SPA)\n" << endl;
	cout << "\t\t\t   [by deBuggers, 2011]\n\n" << endl;

	while (atoi(choice.c_str()) != 0) {
		cout << "****************************************************************\n\n" << endl;
		cout << "1.	Analyze Source Code" << "\n" << endl;
		cout << "2.	Select Query" << "\n" << endl;
		cout << "0.	Quit" << "\n\n" << endl;
		cout << "****************************************************************\n\n" << endl;
		cout << "Your choice: ";
		//getline(cin, choice);

		/////////////////////////////////////////
		choice = "1";
		/////////////////////////////////////////

		if (atoi(choice.c_str()) == 1) {
			cout << "\n\t\t\t 1. Analyze Source Code\n" << endl;
			cout << "\nPlease enter location of your source file: <0 to quit> \n" << endl;
			//getline(cin, source);

			source = "C:\\Users\\Belson\\Desktop\\source.txt";

			if (source.compare("0") == 0) { goto quit; }

			Parser p = Parser(ast);
			DesignExtractor DesExt(ast);
			/*DesExt.fillTable();*/
			status = p.Parse(source);
			cout << endl;

			VarTable *vt = VarTable::getInstance();
			for (INTEGER i = 0; i < vt->getTotalSize(); i++) {
				cout << " " << vt->getVarName(i);
			}
			cout << endl;
			//////////////
			goto quit;
			//////////////
		}
		else if (atoi(choice.c_str()) == 2) {
			switch (status) {
			case STATUS_Success: 
				{
					/***********************************************************************************QUERY*/
					cout << "\n\t\t\t 2. Select Query\n" << endl;
					
					cout<<"\nEnter Declarations: <0 to quit> \n" << endl;
					getline(cin,declaration);

					if (declaration.compare("0") == 0) { goto quit; }

					VarTable *v = VarTable::getInstance();
					StatTable *s = StatTable::getInstance();

					// Call the declaration add function
					SplitDeclaration SpDc; 
					//QueryEvaluator obj;
					//QRYSTATEMENT_TYPE asstType;
					//asstType = SpDc.splitDeclaration(declaration, var);
					//obj.generateMapping(asstType,list);

					cout << "\nEnter your query: <0 to quit> \n" << endl;
					getline(cin,query);

					if (query.compare("0") == 0) { goto quit; }

					// Call the Query Evaluator
		
					//obj.validateQuery(query);
					//obj.executeQuery(output);

					//Print the output- list of integers saved in outputs.
					cout << "Result of your query: <0 to quit> \n";
					for(INTEGER i = 0; i < (INTEGER) output.size(); i++)
						cout<<output.at(i);

					/********************************************************************************END QUERY*/
					break;
				}
			case STATUS_Failure:
				cerr << "The system is unable to process the given source code. Please try again later." << endl;
				break;
			case STATUS_Not_Processed:
				cerr << "\n\t[!] Please provide your source file before querying.\n" << endl;
				break;
			default:
				cerr << "Unknown STATUS: " << status << endl;
			}
		}
		else if (atoi(choice.c_str()) == 0) break;
		else cout << "\n\t\tInvalid choice\n" << endl;
	}
	quit:
		cout << "\n\t[!] SPA is quitting . . .\n" << endl;
	system("PAUSE");
	return 0;
}